home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 14 The Tessellation Stages / BezierPatch / Shaders / BezierTessellation.hlsl next >
Encoding:
Text File  |  2016-03-02  |  4.8 KB  |  193 lines

  1. //***************************************************************************************
  2. // BezierTessellation.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //
  4. // Demonstrates hardware tessellating a Bezier patch.
  5. //***************************************************************************************
  6.  
  7.  
  8.  // Include structures and functions for lighting.
  9. #include "LightingUtil.hlsl"
  10.  
  11. Texture2D    gDiffuseMap : register(t0);
  12.  
  13.  
  14. SamplerState gsamPointWrap        : register(s0);
  15. SamplerState gsamPointClamp       : register(s1);
  16. SamplerState gsamLinearWrap       : register(s2);
  17. SamplerState gsamLinearClamp      : register(s3);
  18. SamplerState gsamAnisotropicWrap  : register(s4);
  19. SamplerState gsamAnisotropicClamp : register(s5);
  20.  
  21. // Constant data that varies per frame.
  22. cbuffer cbPerObject : register(b0)
  23. {
  24.     float4x4 gWorld;
  25.     float4x4 gTexTransform;
  26. };
  27.  
  28. // Constant data that varies per material.
  29. cbuffer cbPass : register(b1)
  30. {
  31.     float4x4 gView;
  32.     float4x4 gInvView;
  33.     float4x4 gProj;
  34.     float4x4 gInvProj;
  35.     float4x4 gViewProj;
  36.     float4x4 gInvViewProj;
  37.     float3 gEyePosW;
  38.     float cbPerObjectPad1;
  39.     float2 gRenderTargetSize;
  40.     float2 gInvRenderTargetSize;
  41.     float gNearZ;
  42.     float gFarZ;
  43.     float gTotalTime;
  44.     float gDeltaTime;
  45.     float4 gAmbientLight;
  46.  
  47.     float4 gFogColor;
  48.     float gFogStart;
  49.     float gFogRange;
  50.     float2 cbPerObjectPad2;
  51.  
  52.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  53.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  54.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  55.     // are spot lights for a maximum of MaxLights per object.
  56.     Light gLights[MaxLights];
  57. };
  58.  
  59. cbuffer cbMaterial : register(b2)
  60. {
  61.     float4   gDiffuseAlbedo;
  62.     float3   gFresnelR0;
  63.     float    gRoughness;
  64.     float4x4 gMatTransform;
  65. };
  66.  
  67. struct VertexIn
  68. {
  69.     float3 PosL    : POSITION;
  70. };
  71.  
  72. struct VertexOut
  73. {
  74.     float3 PosL    : POSITION;
  75. };
  76.  
  77. VertexOut VS(VertexIn vin)
  78. {
  79.     VertexOut vout;
  80.     
  81.     vout.PosL = vin.PosL;
  82.  
  83.     return vout;
  84. }
  85.  
  86. struct PatchTess
  87. {
  88.     float EdgeTess[4]   : SV_TessFactor;
  89.     float InsideTess[2] : SV_InsideTessFactor;
  90. };
  91.  
  92. PatchTess ConstantHS(InputPatch<VertexOut, 16> patch, uint patchID : SV_PrimitiveID)
  93. {
  94.     PatchTess pt;
  95.     
  96.     // Uniform tessellation for this demo.
  97.  
  98.     pt.EdgeTess[0] = 25;
  99.     pt.EdgeTess[1] = 25;
  100.     pt.EdgeTess[2] = 25;
  101.     pt.EdgeTess[3] = 25;
  102.     
  103.     pt.InsideTess[0] = 25;
  104.     pt.InsideTess[1] = 25;
  105.     
  106.     return pt;
  107. }
  108.  
  109. struct HullOut
  110. {
  111.     float3 PosL : POSITION;
  112. };
  113.  
  114. // This Hull Shader part is commonly used for a coordinate basis change, 
  115. // for example changing from a quad to a Bezier bi-cubic.
  116. [domain("quad")]
  117. [partitioning("integer")]
  118. [outputtopology("triangle_cw")]
  119. [outputcontrolpoints(16)]
  120. [patchconstantfunc("ConstantHS")]
  121. [maxtessfactor(64.0f)]
  122. HullOut HS(InputPatch<VertexOut, 16> p, 
  123.            uint i : SV_OutputControlPointID,
  124.            uint patchId : SV_PrimitiveID)
  125. {
  126.     HullOut hout;
  127.     
  128.     hout.PosL = p[i].PosL;
  129.     
  130.     return hout;
  131. }
  132.  
  133. struct DomainOut
  134. {
  135.     float4 PosH : SV_POSITION;
  136. };
  137.  
  138. float4 BernsteinBasis(float t)
  139. {
  140.     float invT = 1.0f - t;
  141.  
  142.     return float4( invT * invT * invT,
  143.                    3.0f * t * invT * invT,
  144.                    3.0f * t * t * invT,
  145.                    t * t * t );
  146. }
  147.  
  148. float3 CubicBezierSum(const OutputPatch<HullOut, 16> bezpatch, float4 basisU, float4 basisV)
  149. {
  150.     float3 sum = float3(0.0f, 0.0f, 0.0f);
  151.     sum  = basisV.x * (basisU.x*bezpatch[0].PosL  + basisU.y*bezpatch[1].PosL  + basisU.z*bezpatch[2].PosL  + basisU.w*bezpatch[3].PosL );
  152.     sum += basisV.y * (basisU.x*bezpatch[4].PosL  + basisU.y*bezpatch[5].PosL  + basisU.z*bezpatch[6].PosL  + basisU.w*bezpatch[7].PosL );
  153.     sum += basisV.z * (basisU.x*bezpatch[8].PosL  + basisU.y*bezpatch[9].PosL  + basisU.z*bezpatch[10].PosL + basisU.w*bezpatch[11].PosL);
  154.     sum += basisV.w * (basisU.x*bezpatch[12].PosL + basisU.y*bezpatch[13].PosL + basisU.z*bezpatch[14].PosL + basisU.w*bezpatch[15].PosL);
  155.  
  156.     return sum;
  157. }
  158.  
  159. float4 dBernsteinBasis(float t)
  160. {
  161.     float invT = 1.0f - t;
  162.  
  163.     return float4( -3 * invT * invT,
  164.                    3 * invT * invT - 6 * t * invT,
  165.                    6 * t * invT - 3 * t * t,
  166.                    3 * t * t );
  167. }
  168.  
  169. // The domain shader is called for every vertex created by the tessellator.  
  170. // It is like the vertex shader after tessellation.
  171. [domain("quad")]
  172. DomainOut DS(PatchTess patchTess, 
  173.              float2 uv : SV_DomainLocation, 
  174.              const OutputPatch<HullOut, 16> bezPatch)
  175. {
  176.     DomainOut dout;
  177.     
  178.     float4 basisU = BernsteinBasis(uv.x);
  179.     float4 basisV = BernsteinBasis(uv.y);
  180.  
  181.     float3 p  = CubicBezierSum(bezPatch, basisU, basisV);
  182.     
  183.     float4 posW = mul(float4(p, 1.0f), gWorld);
  184.     dout.PosH = mul(posW, gViewProj);
  185.     
  186.     return dout;
  187. }
  188.  
  189. float4 PS(DomainOut pin) : SV_Target
  190. {
  191.     return float4(1.0f, 1.0f, 1.0f, 1.0f);
  192. }
  193.